var basePath='http://localhost:8080/';
var submitType=0;//0:添加，1：更新
var customerId;//报修人ID
$(function(){
	initOrderInfo($("#fixOrderId").html());
	initRepairer();
});
//初始化工人下拉框
function initRepairer(){
	$.ajax({
		url:basePath+'workerList',
		dataType:'json',
		data:{},
	    success:function(data){
	    	if(data.code==0){
	    		setRepairer(data.row);
	    	}
	    	else
	    		alert('获取工人信息失败')
	    }
	});
}
function setRepairer(workers){
	var html="";
	for(var i=0;i<workers.length;i++){
		var item=workers[i];
		html+='<option value="'+item.id+'">'+item.name+'</option>';
	}
	$('#repairer').append(html);
}
//初始化订单信息
function initOrderInfo(orderId){
	$.ajax({
		url:basePath+'fixOrder/getInfo',
		method:'get',
		dataType:'json',
		data:{
			fixOrderId:orderId,
		},
		success:function(data){
			if(data.code==0){
				setOrderInfo(data.row);
			}
			else{
				alert('获取订单信息失败')
			}
		},
		error:function(){
			alert("获取报修信息出现错误")
		}
	})
}
function setOrderInfo(info){
	var orderInfo=info.fixOrder;
	var fileList=info.photoList;
	var handleInfo=info.fixOrderHandle;
	setBaseInfo(orderInfo,fileList);
	setHandleInfo(handleInfo);
	//设置提交类型为更新
	if(handleInfo!=null&&handleInfo!=undefined)
		submitType=1;
}
function setBaseInfo(orderInfo,fileList){
	$('#article').val(orderInfo.article);
	$('#description').val(orderInfo.description);
	customerId=orderInfo.accountId;
	$('#name').val(orderInfo.name);
	$('#phone').val(orderInfo.phone);
	$('#address').val(orderInfo.address);
	$('#userRemark').val(orderInfo.remark)
	for(var i=0;i<fileList.length;i++){
		var item=fileList[i];
		var webPath=basePath+item.path+'/'+item.fileName
		var html='<img src="'+webPath+'" class="photo">';
		var index=i+1
		$("#photo"+index).append(html);
	}
}

function setHandleInfo(handleInfo){
    if(handleInfo.examStatus!=null)
	    $('#isRepair'+handleInfo.examStatus).attr("checked","checked");
    if(handleInfo.isCharge!=null)
        $("#isCharge"+handleInfo.isCharge).attr("checked","checked");
	$("#charge").val(handleInfo.charge);
	$("#repairer").val(handleInfo.repairerId);
	$("#fixTime").val(handleInfo.fixTime);
	$('#remark').val(handleInfo.remark)

}
//提交处理信息
function submitHandle(){
	if(validate()==0)
		return false;
	var url=basePath;
	switch(submitType){
	  case 0:url+='fixOrderHandle/addInfo';break;
	  case 1:url+='fixOrderHandle/updateInfo';break;
	};
	var examStatus=$('input[name="isRepair"]:checked').val();
	var isCharge=$('input[name="isCharge"]:checked').val();
	var charge=$("#charge").val();
	if(isCharge==0)
		charge=0;

	$.ajax({
		url:url,
		method:'post',
		dataType:'json',
		data:{
			fixOrderId:$("#fixOrderId").html(),
			examStatus:examStatus,
			isCharge:isCharge,
			charge:charge,
			repairerId:$("#repairer").val(),
			fixTime:$('#fixTime').val(),
			remark:$('#remark').val(),
			accountId:customerId,
		},
		success:function(data){
			if(data.code==0){
				alert("处理成功")
				fixOrderListTopic();
			}
			else
				alert('fail')
		}
	})
}

//校验
function validate(){
	var examStatus=$('input[name="isRepair"]:checked').val();
	if(examStatus==null||examStatus==undefined){
		alert("未选择是否维修");
		return 0;
	}
	var isCharge=$('input[name="isCharge"]:checked').val();
	if(isCharge==null||isCharge==undefined){
		alert("未选择是否收费");
		return 0;
	}
	var repairer=$("#repairer").val();
	if(repairer==""||repairer==undefined){
		alert("未指定维修人员");
		return 0;
	}
	var fixTime=$('#fixTime').val();
	if(fixTime==""||fixTime==undefined){
		alert("未指定维修日期");
		return 0;
	}
	return 1;
}
